<?php

namespace app\shop\controller;

use think\Controller;

class Index extends Controller
{
    protected $ProductModel;
    protected $TypeModel;
    protected $CartModel;

    public function __construct()
    {
        parent::__construct();

        $this->ProductModel = model('Product.Product');
        $this->TypeModel = model('Product.Type');
        $this->CartModel = model('Product.Cart');
    }

    public function index()
    {
        if ($this->request->isGet()) {
            $where = ['status' => '1'];
            // 轮播图
            $swipe = $this->ProductModel->where(['flag' => '3'])->where($where)->limit(5)->select();

            // 分类
            $type = $this->TypeModel->limit(8)->select();

            // 新品首页
            $news = $this->ProductModel->where(['flag' => '1'])->where($where)->limit(8)->select();

            $data = [
                'swipes' => $swipe,
                'types' => $type,
                'news' => $news,
            ];

            $this->success('首页数据', null, $data);
            exit;
        }
    }

    public function types()
    {
        if ($this->request->isGet()) {
            $res = $this->TypeModel->select();

            if (empty($res)) {
                $this->error('暂无分类');
                exit;
            }

            $this->success('分类', null, $res);
            exit;
        }
    }

    public function list()
    {
        if ($this->request->isGet()) {
            $keyword = $this->request->param('keyword', null, 'trim');
            $typeid = $this->request->param('typeid', null, 'intval');
            $flag = $this->request->param('flag', null, 'trim');
            $sort = $this->request->param('sort', 'id', 'trim');
            $by = $this->request->param('by', 'desc', 'trim');
            $page = $this->request->param('page', 1, 'intval');
            $limit = $this->request->param('limit', 8, 'intval');

            $offset = ($page - 1) * $limit;

            $typeName = $this->TypeModel->where(['id' => $typeid])->value('name');
            $typeName = empty($typeName) ? '全部分类' : $typeName;

            $where = [];

            if (!empty($keyword)) {
                $where['name'] = ['like', '%' . $keyword . '%'];
            }

            if (!empty($typeid)) {
                $where['typeid'] = $typeid;
            }

            if (!empty($flag)) {
                $where['flag'] = $flag;
            }

            

            $count = $this->ProductModel
                ->where($where)
                ->where(['status' => '1'])
                ->order($sort, $by)
                ->limit($offset, $limit)
                ->count();

            $list = $this->ProductModel
                ->where($where)
                ->where(['status' => '1'])
                ->order($sort, $by)
                ->limit($offset, $limit)
                ->select();

            if (empty($list)) {
                $this->error('暂无更多商品数据');
                exit;
            } else {
                $data = [
                    'list' => $list,
                    'typeName' => $typeName,
                    'count' => $count,
                ];

                $this->success('获取商品数据成功', null, $data);
                exit;
            }
        }
    }

    // 商品详情
    public function product()
    {
        if ($this->request->isGet()) {
            $proid = $this->request->param('proid', 0, 'trim');

            $result = $this->ProductModel->with(['type', 'unit'])->where(['status' => '1'])->find($proid);

            if($result){
                $this->success('获取商品详情成功', null, $result);
                exit;
            }else{
                $this->error('商品不存在');
                exit;
            }
        }
    }

    // 购物车内商品数量
    public function cartCount()
    {
        if($this->request->isGet()){
            $busid = $this->request->param('busid', 0, 'trim');

            $count = $this->CartModel->where('busid', $busid)->count();

            $this->success('获取购物车商品数量成功', null, $count);
            exit;
        }
    }
}
